/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2020-01-10 17:30:23  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.risk.repository;

import java.util.List;

import com.joysuch.wwyt.risk.entity.RiskEvaluationRepo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.query.Param;

/**
 * RiskEvaluationRepoDao
 * 
 */
public interface RiskEvaluationRepoDao
        extends JpaRepository<RiskEvaluationRepo, Long>, QuerydslPredicateExecutor<RiskEvaluationRepo> {
    public Page<RiskEvaluationRepo> findAll(Specification<RiskEvaluationRepo> spec, Pageable pageable);

    public RiskEvaluationRepo save(RiskEvaluationRepo bean);

    public void delete(RiskEvaluationRepo bean);

    @Modifying
    @Query("update RiskEvaluationRepo set state=:state where workSafetyCertId is NULL and riskPoint.id=:riskPointId and type=:type and id != :excludeId")
    public void updateStateForStaticEvaluationOfRiskPoint(@Param("riskPointId") Long riskPointId,
            @Param("type") String type, @Param("state") int state, @Param("excludeId") Long excludeId);

    @Modifying
    @Query("update RiskEvaluationRepo set state=:state where riskPoint.id=:riskPointId and type=:type and id != :excludeId")
    public void updateStateForEvaluationOfRiskPoint(@Param("riskPoint") Long riskPointId, @Param("type") String type,
            @Param("state") int state, @Param("excludeId") Long excludeId);

    public List<RiskEvaluationRepo> findByRiskPointIdAndState(Long riskPointId, int state);

    @Query("select m from RiskEvaluationRepo m where riskPoint.id=?1 and type=?2 and state=?3 order by riskLevel asc,createTime desc")
    public List<RiskEvaluationRepo> findMostImportantRepoForRiskPoint(Long riskPointId, String type, int state);

    @Query("select m from RiskEvaluationRepo m where riskPoint.id=?1 and type=?2 and state=?3 and workSafetyCertId is NULL order by riskLevel asc,createTime desc")
    public List<RiskEvaluationRepo> findMostImportantRepoNotFromWorkSafetyCertForRiskPoint(Long riskPointId,
            String type, int state);

    @Query("select evaluationId from RiskEvaluationRepo m where  type=?1")
    public List<Long> findAllEvaluationIdOfType(String type);

    public RiskEvaluationRepo findFirstByTypeAndStateOrderByIdDesc(String type, Integer state);

    @Query("select min(m.riskLevel) from RiskEvaluationRepo m, RiskPoint p where m.riskPoint.id=p.id and p.shared=1 and m.riskDistrictId=?1 and m.state=?2")
    public Integer findMostImportWorkSafetyCertRiskLevelOfDistrict(Long riskDistrictId, Integer state);

    public RiskEvaluationRepo findByEvaluationId(Long id);
    public RiskEvaluationRepo findByWorkSafetyCertId(Long id);
}
